package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.Outlist
import com.cheyun.netsalev3.bean.OutlistParam
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class OverdueActivityPageRepository(var date: String) : BasePageRepository<OutlistParam>() {
    var uid = 0
    init {
        MySharedPreferences.getLoginData()?.member?.uid?.apply {
            uid = this
        }
    }

    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<OutlistParam>) -> Unit) {
        ApiClient.getInstance().getOutlist(1, date,uid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Outlist>>() {
                    override fun onSuccess(t: BaseResp<Outlist>) {
                        t.data?.rows?.apply {
                            operXphone(this)
                            callback(this)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }

                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, OutlistParam>) {
        ApiClient.getInstance().getOutlist(params.key, date,uid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Outlist>>() {
                    override fun onSuccess(t: BaseResp<Outlist>) {
                        t.data?.rows?.apply {
                            operXphone(this)
                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }

                })

    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, OutlistParam>) {
        ApiClient.getInstance().getOutlist(params.key, date,uid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Outlist>>() {
                    override fun onSuccess(t: BaseResp<Outlist>) {
                        t.data?.rows?.apply {
                            operXphone(this)
                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }

    fun operXphone(data: MutableList<OutlistParam>){
        data.forEach {
            if(it.xphone.isNullOrEmpty()){
                it.xphone = it.phone
            }
        }
    }

}